package com.neborosoft.lib.collections.stack;

/**
 * Created with IntelliJ IDEA.
 * User: CM
 * Date: 12.01.13
 * Time: 6:45
 * To change this template use File | Settings | File Templates.
 */

import com.neborosoft.lib.API_8.LinkedList;

public class HistoryStack<T> implements Stack<T> {
    private LinkedList<T> stack;
    int maxSize;

    public HistoryStack(int maxSize){
        stack = new LinkedList<T>();
        this.maxSize = maxSize;
    }

    @Override
    public void push(T element){
        if(stack.size() >= maxSize){
            stack.removeLast();
        }
        stack.push(element);
    }

    @Override
    public boolean isEmpty(){
        return stack.isEmpty();
    }

    @Override
    public T pop(){
        if(isEmpty()){
            throw new EmptyStackException();
        }
        return stack.pop();
    }

    @Override
    public T top(){
        if(isEmpty()){
            throw new EmptyStackException();
        }
        return stack.getFirst();
    }
}

